package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dao.OmsCompanyAddressDao;
import com.macro.mall.mapper.OmsCompanyAddressMapper;
import com.macro.mall.model.OmsCompanyAddress;
import com.macro.mall.model.OmsCompanyAddressExample;
import com.macro.mall.service.OmsCompanyAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 收货地址管理Service实现类
 * Created by macro on 2018/10/18.
 */
@Service
public class OmsCompanyAddressServiceImpl implements OmsCompanyAddressService {
    @Autowired
    private OmsCompanyAddressMapper companyAddressMapper;
    @Autowired
    private OmsCompanyAddressDao omsCompanyAddressDao;
    @Override
    public List<OmsCompanyAddress> list() {
        return companyAddressMapper.selectByExample(new OmsCompanyAddressExample());
    }

    @Override
    public List<OmsCompanyAddress> pageList(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return companyAddressMapper.selectByExample(null);
    }

    @Override
    public int create(OmsCompanyAddress omsCompanyAddress) {
        List<OmsCompanyAddress> omsCompanyAddresses = companyAddressMapper.selectByExample(null);
        List<Long> ids = omsCompanyAddresses.stream().map(x -> x.getId()).collect(Collectors.toList());
        if (omsCompanyAddress.getSendStatus()==1 && omsCompanyAddress.getReceiveStatus()==1){
            omsCompanyAddressDao.updateGetById(ids,0,0);
        }
        if (omsCompanyAddress.getSendStatus()==1){
            omsCompanyAddressDao.updateGetById(ids,0,null);
        }
        if(omsCompanyAddress.getReceiveStatus()==1){
            omsCompanyAddressDao.updateGetById(ids,null,0);
        }
        return companyAddressMapper.insertSelective(omsCompanyAddress);
    }

    @Override
    public int update(OmsCompanyAddress omsCompanyAddress) {
        List<OmsCompanyAddress> omsCompanyAddresses = companyAddressMapper.selectByExample(null);
        List<Long> ids = omsCompanyAddresses.stream().map(x -> x.getId()).collect(Collectors.toList());
        if (omsCompanyAddress.getSendStatus()==1 && omsCompanyAddress.getReceiveStatus()==1){
            omsCompanyAddressDao.updateGetById(ids,0,0);
        }
        if (omsCompanyAddress.getSendStatus()==1){
            omsCompanyAddressDao.updateGetById(ids,0,null);
        }
        if(omsCompanyAddress.getReceiveStatus()==1){
            omsCompanyAddressDao.updateGetById(ids,null,0);
        }
        return companyAddressMapper.updateByPrimaryKey(omsCompanyAddress);
    }

    @Override
    public int delete(Long id) {
        return companyAddressMapper.deleteByPrimaryKey(id);
    }

}
